package com.karashi.test.local;

import java.io.File;
import java.util.List;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

public class HtmlParserLocal {

	private static final String XPATH_CONTENTS_HOME = "//div[@id='contents'][last()]";
	private static final String SEARCH_RESULT = XPATH_CONTENTS_HOME + "/h3/img/@alt";

	public static void main(String[] args) {
		System.out.println(new HtmlParserLocal().get_2().toString());
	}

	public StringBuilder get_2() {
		StringBuilder sb = new StringBuilder();

		// config cleaner properties

		HtmlCleaner htmlCleaner = new HtmlCleaner();

		CleanerProperties props = htmlCleaner.getProperties();
		props.setAllowHtmlInsideAttributes(false);
		props.setAllowMultiWordAttributes(true);
		props.setRecognizeUnicodeChars(true);
		props.setOmitComments(true);

		// create URL object
		try {
			// get HTML page root node

			// File file = new File(
			// "C:\\Users\\k-kouno\\dev\\workspace3.7\\BusLocationSystemTestPrj\\test_sample\\response_sample_1.html");
			File file = new File(
					"C:\\Users\\karakuchi7\\dev\\workspace\\BusLocationSystemTestPrj\\test_sample\\response_sample_1.html");

			TagNode root = htmlCleaner.clean(file);

			Object[] nodes = root.evaluateXPath(SEARCH_RESULT);
			if (nodes.length > 0) {
				if ("検索エラー".equals(nodes[0].toString())) {
					sb.append(nodes[0].toString());
				} else {
					Object[] contentsHomeNodes = root.evaluateXPath(XPATH_CONTENTS_HOME);

					if (contentsHomeNodes.length > 0) {
						TagNode contentsHomeNode = (TagNode) contentsHomeNodes[0];

						/* 現在時刻取得 */
						sb.append(contentsHomeNode.evaluateXPath("//.[@class='src-time']/text()")[0].toString());
						sb.append("\n");

						/* ルート取得 */
						sb.append(contentsHomeNode.evaluateXPath("//.[@class='src-route']/./text()")[0].toString());
						sb.append("\n");

						/* 検索結果取得 */
						TagNode tableTag = (TagNode) contentsHomeNode.evaluateXPath("//.[@class='src-dia']/tbody")[0];
						sb.append("\n");

						List childTagList = tableTag.getChildTagList();

						int i = 0;
						for (Object object : childTagList) {
							TagNode tagNode = (TagNode) object;

							if (i == 0) {
								// テーブルヘッダ
								sb.append("### ヘッダ ###");
								sb.append("\n");
								for (Object tableHeader : tagNode.getChildTagList()) {
									sb.append(((TagNode) tableHeader).getText()).append(" : ");
								}
								sb.append("\n");
							} else {
								// 検索結果
								sb.append("### 結果" + i + " ###");
								sb.append("\n");
								for (Object tableHeader : tagNode.getChildTagList()) {
									sb.append(((TagNode) tableHeader).getText()).append(" : ");
								}
								
								// soonバスか判定
								if("soon".equals(tagNode.getChildTags()[0].getAttributeByName("class"))){
									sb.append("*");
								}
								
								sb.append("\n");

							}

							i++;
						}

						sb.append("\n");
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return sb;
	}
}
